package cn.huiyunche.admin.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.huiyunche.base.service.interfaces.TruckInfoService;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.tools.basic.exceptions.BusinessException;

@RequestMapping("/usertruck")
@Controller
public class UserAduitController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(UserAduitController.class);
	
	@Autowired
	private TruckInfoService truckInfoService = null;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> list(PageVo page) {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		try {
			result.setData(this.getTruckInfoService().list(page));
		} catch (Exception e) {
			LOGGER.error("UserAduitController.list error: {}", e);
			throw new BusinessException("查询异常");
		}
		return result;
	}

	/**
	 * 车辆审核
	 * 
	 * @param id 主键ID
	 * @param status 状态（10:审核中，20:审核未通过，30：审核已通过）
	 * @param userid 用户ID
	 * @return
	 */
	@RequestMapping(value = "/audit", method = RequestMethod.POST)
	@ResponseBody
	public Result<Integer> audit(String truckId, String driverId, String status, Boolean truckType){
		Result<Integer> result = new Result<Integer>(true, null, "审核成功");
		try {
			result.setData(this.getTruckInfoService().auditUser(truckId, driverId, status, truckType));
		} catch (Exception e) {
			LOGGER.error("UserAduitController.audit error: {}", e);
			throw new BusinessException("审核异常");
		}
		return result;
	}

	@RequestMapping(value = "/truckDetail/{truckId}", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> truckDetail(@PathVariable("truckId") String truckId) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		try {
			result.setData(this.getTruckInfoService().queryTruckInfo(Long.valueOf(truckId)));
		} catch (Exception e) {
			LOGGER.error("UserAduitController.truckDetail error: {}", e);
			throw new BusinessException("查询异常");
		}
		return result;
	}
	
	@RequestMapping(value = "/driverDetail/{driverId}", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> driverDetail(@PathVariable("driverId") String driverId) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		try {
			result.setData(this.getTruckInfoService().queryDriverInfo(Long.valueOf(driverId)));
		} catch (Exception e) {
			LOGGER.error("UserAduitController.driverDetail error: {}", e);
			throw new BusinessException("查询异常");
		} 
		return result;
	}

	@RequestMapping(value = "/loadpic/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> loadPic(@PathVariable("id") String id) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		try {
			result.setData(this.getTruckInfoService().loadPic(id));
		} catch (Exception e) {
			LOGGER.error("UserAduitController.loadPic error: {}", e);
			throw new BusinessException("图片加载异常");
		}
		return result;
	}

	private TruckInfoService getTruckInfoService() {
		return this.truckInfoService;
	}
}
